using UnityEngine;
using System.Collections;

public class level_controller : MonoBehaviour {

	public Object						unit_prefab;
	private Vector3						trans;
	
	// Use this for initialization
	void Awake () {
		GameObject[] spawn_points 		= GameObject.FindGameObjectsWithTag("Respawn");
		
		foreach (GameObject i in spawn_points)	
			spawn_unit					(i);

		new 							e_server();
		register_entities				();
		
		trans 							= new Vector3(0.0f, 0.0f, 0.0f);
	}
	
	void Update () {
		foreach (GameObject i in GameObject.FindGameObjectsWithTag("Unit"))
		{
			entity_unit unit			= i.GetComponent<entity_unit>();
			trans.x						= unit.speed * unit.Sx * unit.Dx * Time.deltaTime;
			i.transform.Translate		(trans);
		}	
	}
	
	private void 	register_entities	()
	{
		foreach (entity e in GameObject.FindObjectsOfType(typeof(entity)))
		{
			e_server.instance.register(e);
		}
	}

	private void	spawn_unit			(GameObject spawn_point)
	{
		GameObject 	subj 				= (GameObject) Instantiate(unit_prefab, spawn_point.transform.position, Quaternion.identity);
		entity_unit unit 				= subj.GetComponent<entity_unit>();
		respawn 	point				= spawn_point.GetComponent<respawn>();
		
		unit.Team						= point.team;
		unit.Dx							= point.direction;
	}
}
